package com.itboye.mengwu.app.ui.roof;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Observable;
import java.util.Observer;

import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.config.GlobalConfig;
import com.itboye.core.alert.ByAlert;
import com.itboye.core.object.BaseActivity;
import com.itboye.core.object.ResultEntity;
import com.itboye.core.utils.XImageLoader;
import com.itboye.mengwu.R;
import com.itboye.mengwu.app.MainApplication;
import com.itboye.mengwu.app.adapter.DapeigouAdapter;
import com.itboye.mengwu.app.ui.main.GoodsDetailActivity;
import com.itboye.mengwu.buy.ui.DapeigouQueRenDingDan;
import com.itboye.mengwu.data.presenter.OrderPresenter;
import com.itboye.mengwu.ui.shop.DaPaiGouRootBean;
import com.itboye.mengwu.ui.shop.DapPeiGouJiaRuGouWuCheDialog;
import com.itboye.mengwu.ui.shop.DaPaiGouRootBean.A;

public class DaPeiGouDetailActivity extends BaseActivity implements
		OnClickListener, Observer {

	ListView listView;
	TextView txt_title;
	ImageView back;
	OrderPresenter orderPresenter;
	private String packageId;
	private DaPaiGouRootBean bean;
	private DapeigouAdapter adapter;
	TextView jiezhang;
	Button jiesuan;
	LinearLayout goodsContainer;
	ArrayList<TextView> arrayListShuxing = new ArrayList<TextView>();
	ArrayList<ImageView> arrayListImage = new ArrayList<>();
	ArrayList<TextView> arrayListCount = new ArrayList<TextView>();
	ArrayList<String> counts = new ArrayList<>();
	ArrayList<String> pid = new ArrayList<>();

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.activity_dapeigou;
	}

	@Override
	public void onMyCreate() {
		// TODO Auto-generated method stub
		IntentFilter filter = new IntentFilter("select");
		registerReceiver(broadcastReceiver, filter);
		packageId = getIntent().getStringExtra("packageId");
		orderPresenter = new OrderPresenter(this);
		orderPresenter.dapeigou(packageId);
	}

	BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
		public void onReceive(android.content.Context context, Intent intent) {
			arrayListShuxing.get(intent.getIntExtra("position", -1)).setText(
					intent.getStringExtra("skuDesc"));
			bean.getItems().get(intent.getIntExtra("position", -1))
					.getSkuInfo().setId(intent.getStringExtra("id"));
			bean.getItems().get(intent.getIntExtra("position", -1))
					.getSkuInfo().setSkuDesc(intent.getStringExtra("skuDesc"));
			if (!TextUtils.isEmpty(getIntent().getStringExtra("img")))
				XImageLoader.load(GlobalConfig.API_URL + "/picture/index?id="
						+ getIntent().getStringExtra("img"),
						arrayListImage.get(intent.getIntExtra("position", -1)));
			arrayListShuxing.get(intent.getIntExtra("position", -1)).setText(
					intent.getStringExtra("skuDesc"));
			arrayListShuxing.get(intent.getIntExtra("position", -1)).setTag(
					intent.getStringExtra("id"));
			// ByAlert.alert("广播这边的"+arrayListShuxing.get(intent.getIntExtra("position",
			// -1)).getTag());
			arrayListCount.get(intent.getIntExtra("position", -1)).setText(
					"x" + intent.getIntExtra("count", 1));
			arrayListCount.get(intent.getIntExtra("position", -1)).setTag(
					intent.getIntExtra("count", 1));
		};
	};
	private boolean hasSelect;

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.back:
			finish();
			break;
		case R.id.jiesuan:
			for (int i = 0; i < arrayListShuxing.size(); i++) {
				if (arrayListShuxing.get(i).getTag() == null) {
					hasSelect = false;
					break;
				} else {
					hasSelect = true;
				}
				System.out.println(arrayListShuxing.get(i).getText().toString()
						+ ":" + arrayListShuxing.get(i).getTag());
			}
			if (!hasSelect) {
				ByAlert.alert("请先选择商品属性");
				return;
			}
			for (int i = 0; i < arrayListCount.size(); i++) {
				counts.add(arrayListCount.get(i).getTag() + "");
			}
			Intent intent = new Intent(MainApplication.context,
					DapeigouQueRenDingDan.class);//
			intent.putExtra("model", bean);

			intent.putExtra("isLiJiBuy", true);
			intent.putExtra("counts", counts);
			if (bean != null) {
				startActivity(intent);
			} else {

			}
			break;
		default:
			break;
		}
	}

	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = handlerError(data);
		if (entity != null) {
			if (entity.getEventType() == OrderPresenter.DapeigouSuccess) {
				// ByAlert.alert(entity.getData() + "");
				bean = (DaPaiGouRootBean) entity.getData();
				for (int i = 0; i < bean.getItems().size(); i++) {
					final A items = bean.getItems().get(i);
					View view = LayoutInflater.from(this).inflate(
							R.layout.item_dapeigou, null);
					TextView name = (TextView) view
							.findViewById(R.id.txt_goodsName);
					TextView price = (TextView) view.findViewById(R.id.price);
					RelativeLayout item_rel = (RelativeLayout) view
							.findViewById(R.id.item_rel);
					LinearLayout select = (LinearLayout) view
							.findViewById(R.id.select);
					TextView shuxing = (TextView) view
							.findViewById(R.id.shuxing);
					TextView count = (TextView) view
							.findViewById(R.id.old_price);
					count.setTag("1");
					arrayListShuxing.add(shuxing);
					arrayListCount.add(count);
					ImageView img_goods = (ImageView) view
							.findViewById(R.id.img_goods);
					XImageLoader.load(GlobalConfig.API_URL
							+ "/picture/index?id=" + items.getMainImg(),
							img_goods);
					pid.add(items.getPId());
					item_rel.setTag(i);
					name.setText(items.getName());
					price.setVisibility(View.GONE);
					select.setTag(i);
					if (items.getSkuInfo().getTemp() == null) {// 单规格
						shuxing.setText(items.getSkuInfo().getSkuDesc());
						shuxing.setTag(items.getSkuInfo().getId());
					} else {
						select.setOnClickListener(new OnClickListener() {

							@Override
							public void onClick(View v) {
								// TODO Auto-generated method stub
								DapPeiGouJiaRuGouWuCheDialog dapPeiGouJiaRuGouWuCheDialog = new DapPeiGouJiaRuGouWuCheDialog(
										(int) v.getTag(), items, false);
								dapPeiGouJiaRuGouWuCheDialog.show(
										getSupportFragmentManager(), "");
							}
						});
					}
					item_rel.setOnClickListener(new OnClickListener() {

						@Override
						public void onClick(View v) {
							// TODO Auto-generated method stub
							Intent intent = new Intent(
									DaPeiGouDetailActivity.this,
									GoodsDetailActivity.class);
							intent.putExtra("pid", pid.get((int) v.getTag()));
							intent.putExtra("dapeigou", "dapeigou");
							startActivity(intent);
						}
					});
					goodsContainer.addView(view);
				}

				txt_title.setText(bean.getPackageName());
				jiezhang.setText(Html.fromHtml("合计 共："
						+ "<font color='#FF6EA5'>￥" + bean.getPackagePrice()
						+ "</font>"));
				// adapter = new DapeigouAdapter(this, bean);
				// listView.setAdapter(adapter);
			} else if (entity.getEventType() == OrderPresenter.DapeigouFail) {
				ByAlert.alert(entity.getData() + "");
			}
		}
	}

}
